package com.lgkj.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.lgkj.R;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.view.CountDownTimerButton;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/6/26 0026.
 */

public class RegisterActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_password_config)
    EditText etPasswordConfig;
    @BindView(R.id.cb_pwd)
    CheckBox cbPwd;
    @BindView(R.id.et_ver)
    EditText etVer;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_ver)
    CountDownTimerButton btnVer;

    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.iv_back, R.id.cb_pwd, R.id.tv_xieyi, R.id.ibtn_save, R.id.tv_login, R.id.btn_ver})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.cb_pwd:
                if (cbPwd.isChecked()) {
                    etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                etPassword.setSelection(etPassword.getText().toString().length());
                break;
            case R.id.tv_xieyi:
                Intent intent = new Intent(RegisterActivity.this, XieYiActivity.class);
                intent.putExtra("title", "协议");
                intent.putExtra("url", "http://www.lianguangtech.com/lg/about/index.html");
                startsActivity(intent);
                break;
            case R.id.ibtn_save:
                if (!CommonUtils.isPhone(etPhone)) {
                    toastLong("请输入正确的电话号码", 0);
                    return;
                }
                if (TextUtils.isEmpty(etPassword.getText())) {
                    toastLong("请先输入密码", 0);
                    return;
                }
                if(!etPassword.getText().toString().equals(etPasswordConfig.getText().toString())){
                    toastLong("两次输入密码不相同", 0);
                    return;
                }
                if (TextUtils.isEmpty(etVer.getText())) {
                    toastLong("请先输入验证码", 0);
                }
                register();
                break;
            case R.id.tv_login:
                startsActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                break;
            case R.id.btn_ver:
                getVer();
                break;
        }
    }

    private void register() {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(RegisterActivity.this));
        map.put("phone", etPhone.getText().toString());
        map.put("password", etPassword.getText().toString());
        map.put("verify", etVer.getText().toString());
        map.put("active_code", etCode.getText().toString());
        HttpUtils.postHttp(map, CommonConstant.register, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("code") == 0) {
                    toastLong("注册成功,请直接登录", 0);
                    finish();
                } else {
                    toastLong(jsonObject.optString("msg"), 0);

                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }

    private void getVer() {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(RegisterActivity.this));
        map.put("mobile", etPhone.getText().toString());
        HttpUtils.postHttp(map, CommonConstant.getver, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("code") == 0) {
                    toastLong("验证码发送成功，请注意查收", 0);
                    btnVer.start();
                } else {
                    toastLong(jsonObject.optString("msg"), 0);

                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
